Enum in C
Enum
stands for enumeration which is a user defined type in C. It is used to assign names to integral constants which makes a program easy to read.
Example
enum status = {Present = 0, Absent = 1};
The enumeration starts with the keyword enum
, then we will assign an integer to the value. If no value is assigned then the default integer starts with 0 and incremented by 1 for subsequent values.
Example
enum weekdays {
Sunday = 0,
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6
};
Example Program
#include<stdio.h>
enum weekdays {
Sunday = 0,
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6
};
int main()
{
enum weekdays day;
day = Monday;
printf("%d",day);
return 0;
}
Output
1